package com.jack.lib.test.view

import android.annotation.SuppressLint
import android.content.Context
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.reflect.TypeToken
import com.hjq.gson.factory.GsonFactory
import com.jack.lib.core.config.Constant
import com.jack.lib.core.ext.toJson
import com.jack.lib.core.ext.toastW
import com.jack.lib.test.R
import com.jack.lib.test.bean.Host
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.tencent.mmkv.MMKV


/**
 * author : chong.huang
 * create : 2022/2/5
 * desc   :
 */
class HostDialog(context: Context) : BottomPopupView(context) {

    private val adapter by lazy { object : BaseQuickAdapter<Host, BaseViewHolder>(R.layout.adapter_host) {
        @SuppressLint("NotifyDataSetChanged")
        override fun convert(holder: BaseViewHolder, item: Host) {
            holder.setText(R.id.host, item.host)
            holder.getView<CheckBox>(R.id.checkbox).isChecked = item.select
            holder.itemView.setOnClickListener {
                if (!item.select) {
                    XPopup.Builder(getContext()).asConfirm(
                        "提示", "是否切换"
                    ) {
                        data.forEach { it.select = false }
                        item.select = true
                        notifyDataSetChanged()
                        changeHost(item)
                        saveData()
                        AppUtils.relaunchApp(true)
                    }.show()
                }
            }
        }
    } }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_host
    }

    override fun onCreate() {
        super.onCreate()
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = adapter

        findViewById<TextView>(R.id.add).setOnClickListener {
            XPopup.Builder(context).asInputConfirm(
                "添加host", "请输入host, 例如: http://www.aaaa.bbb:8080"
            ) { text ->
                if (!text.startsWith("http")) {
                    return@asInputConfirm toastW("host 输入有误, 是否是 http:// ?")
                }
                if (!RegexUtils.isURL(text)) {
                    return@asInputConfirm toastW("请输入正确的host")
                }
                adapter.addData(Host(text))
                saveData()
            }.show()
        }

        adapter.setList(getData())
    }

    private fun changeHost(host: Host) {
        MMKV.defaultMMKV()?.encode(Constant.HOST, host.host)
    }

    private fun saveData() {
        MMKV.defaultMMKV()?.encode(Constant.HOST_LIST, adapter.data.toJson())
    }

    private fun getData(): List<Host> {
        val hostList = MMKV.defaultMMKV()?.decodeString(Constant.HOST_LIST)?: defaultHost()
        return GsonFactory.getSingletonGson().fromJson(hostList, object : TypeToken<List<Host>>() {}.type)
    }

    private fun defaultHost(): String {
        return listOf(
            Host(MMKV.defaultMMKV()?.decodeString(Constant.HOST)?: "", true),
            Host("https://www.hbgtw.com.cn/steeladaper/")
        ).toJson()
    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.6f).toInt()
    }
}